package com.xr.miniprogram.controller.user;

import com.xr.common.core.controller.BaseController;
import com.xr.common.core.domain.AjaxResult;
import com.xr.miniprogram.domain.MiniProgramUser;
import com.xr.miniprogram.domain.MiniProgramUserAddress;
import com.xr.miniprogram.domain.vo.MiniProgramUserVO;
import com.xr.miniprogram.service.IMiniProgramUserAddressService;
import com.xr.miniprogram.service.IMiniProgramUserService;
import com.xr.framework.miniprogram.service.UserTokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api("小程序用户控制")
@RestController
@RequestMapping("/miniprogram/user")
public class MiniProgramUserController extends BaseController {
    @Resource
    private IMiniProgramUserService userService;
    @Resource
    private IMiniProgramUserAddressService userAddressService;

    @Resource
    UserTokenService userTokenService;

    /**
     * 用户登录，包含注册
     * @param miniProgramUserVO 目前仅小程序登录，传输一个code
     * @return
     */
    @ApiOperation("微信用户登录、注册")
    @PostMapping("/login")
    public AjaxResult login(@ApiParam(value = "用户登录对象", required = true) @RequestBody MiniProgramUserVO miniProgramUserVO){
        MiniProgramUser  loginUser = userService.login(miniProgramUserVO);
        MiniProgramUserVO miniProgramUserVO1 = MiniProgramUserVO.toMiniProgramUserVO(loginUser);
        miniProgramUserVO1.setToken(userTokenService.createToken(miniProgramUserVO));
        return success(miniProgramUserVO1);
    }

    /**
     * 更新用户
     * @param miniProgramUser
     * @return
     */
    @ApiOperation("修改用户信息")
    @PostMapping("/update")
    public AjaxResult update(@ApiParam(value = "用户对象", required = true) @RequestBody MiniProgramUser miniProgramUser){
        return toAjax(userService.updateUser(miniProgramUser));
    }

    /**
     * 根据用户Id获取信息
     * @param miniProgramUserId
     * @return
     */
    @ApiOperation("根据id获取用户")
    @GetMapping("/{miniProgramUserId}")
    public AjaxResult getByUserId(@ApiParam(value = "用户id", required = true) @PathVariable("miniProgramUserId") Long miniProgramUserId){
        return success(userService.selectMiniProgramUserByMiniProgramUserId(miniProgramUserId));
    }

    /**
     * 获取用户地址列表
     * @param miniProgramUserId 用户id
     * @return 地址列表
     */
    @ApiOperation("获取用户地址列")
    @GetMapping("/address/list/{miniProgramUserId}")
    public AjaxResult getUserAddressList(@ApiParam(value = "用户id", required = true) @PathVariable("miniProgramUserId")Long miniProgramUserId){
        List<MiniProgramUserAddress> userAddressList = userAddressService.queryUserAddressList(miniProgramUserId);
        return AjaxResult.success(userAddressList);
    }

    /**
     * 根据id获取地址
     * @param miniProgramUserAddressId 地址id
     * @return 地址信息
     */
    @ApiOperation("根据id获取地址")
    @GetMapping("/address/one/{miniProgramUserAddressId}")
    public AjaxResult getUserAddressById( @ApiParam(value = "用户地址id", required = true) @PathVariable("miniProgramUserAddressId")Long miniProgramUserAddressId){
        MiniProgramUserAddress userAddress = userAddressService.queryUserAddressById(miniProgramUserAddressId);
        return AjaxResult.success(userAddress);
    }

    /**
     * 新增或更新地址
     * @param miniProgramUserAddress 地址类
     * @return
     */
    @ApiOperation("新增或更新地址")
    @PostMapping("/address/addOrUpdate")
    public AjaxResult addUserAddress(@ApiParam(value = "用户地址对象", required = true) @Validated(MiniProgramUserAddress.add.class) @RequestBody MiniProgramUserAddress miniProgramUserAddress){
        return success( userAddressService.saveOrUpdateAddress(miniProgramUserAddress));
    }

    /**
     * 根据用户id获取默认地址
     * @param userId miniProgramUserId
     * @return 地址信息
     */
    @ApiOperation("根据用户id获取默认地址")
    @GetMapping("/address/default/{userId}")
    public AjaxResult defaultAddress(@ApiParam(value = "用户id", required = true) @PathVariable("userId") Long userId){
        MiniProgramUserAddress miniProgramUserAddress= userAddressService.defaultAddress(userId);
        return success(miniProgramUserAddress);
    }

    /**
     * 更新默认地址
     * @param miniProgramUserAddress miniProgramUserId, miniProgramUserAddressId
     * @return
     */
    @ApiOperation("修改默认地址")
    @PostMapping("/address/changeDefault")
    public AjaxResult changeDefault(@ApiParam(value = "地址对象", required = true) @RequestBody MiniProgramUserAddress miniProgramUserAddress){
        return success(userAddressService.changeDefault(miniProgramUserAddress));
    }

    /**
     * 删除地址
     * @param addressId 地址id
     * @return
     */
    @ApiOperation("根据id删除地址")
    @PostMapping("/address/delete/{addressId}")
    public AjaxResult delete(@ApiParam(value = "地址id", required = true) @PathVariable("addressId") Long addressId){
        return success(userAddressService.removeById(addressId));
    }
}
